import java.util.*;

/**
 * @author caiwl
 * @date 2020/11/4 16:03
 */
public class TreeDemo {
    public static void main(String[] args) {
        List<Record> data = new ArrayList();
        data.add(new Record("安全系统", "安全管理", "人员", "各矿井下超时工作人员"));
        data.add(new Record("管理系统", "调度系统", "发电量", "发电量仪表盘"));
        data.add(new Record("管理系统", "调度系统", "大屏标题", "各单位非正常作业工作票..."));
        data.add(new Record("管理系统", "调度系统", "新勤哲指标", "月原煤生产逐日图"));
        data.add(new Record("管理系统", "调度系统", "非正常作业工作票", "各单位非正常作业工作票..."));

        // 第一层
        List<TreeNode> result = new ArrayList<>();
        String systemType;
        Set<String> set = new HashSet<>();
        for (Record record : data) {
            systemType = record.getSystemType();
            if (set.add(systemType)) {
                TreeNode node = new TreeNode();
                node.setId(systemType);
                node.setText(systemType);
                node.setChildren(systemName(systemType, data));
                result.add(node);
            }
        }

        System.out.println(result.toString());
    }

    // 第二层
    private static List<TreeNode> systemName(String systemType, List<Record> data) {
        List<TreeNode> systemNames = new ArrayList<>();
        Set<String> set = new HashSet<>();
        String systemName;
        for (Record record : data) {
            systemName = record.getSystemName();
            if (Objects.equals(systemType, record.getSystemType())
                    && set.add(systemName)) {
                TreeNode node = new TreeNode();
                node.setId(systemName);
                node.setText(systemName);
                node.setChildren(module(systemType, systemName, data));
                systemNames.add(node);
            }
        }
        return systemNames;
    }

    // 第三层
    private static List<TreeNode> module(String systemType, String systemName, List<Record> data) {
        List<TreeNode> modules = new ArrayList<>();
        Set<String> set = new HashSet<>();
        String module;
        for (Record record : data) {
            module = record.getModule();
            if (Objects.equals(systemType, record.getSystemType())
                    && Objects.equals(systemName, record.getSystemName())
                    && set.add(module)) {
                TreeNode node = new TreeNode();
                node.setId(module);
                node.setText(module);
                node.setChildren(showTitle(systemType, systemName, module, data));
                modules.add(node);
            }
        }
        return modules;
    }

    // 第四层
    private static List<TreeNode> showTitle(String systemType, String systemName, String module, List<Record> data) {
        List<TreeNode> showTitles = new ArrayList<>();
        Set<String> set = new HashSet<>();
        String showTitle;
        for (Record record : data) {
            showTitle = record.getShowTitle();
            if (Objects.equals(systemType, record.getSystemType())
                    && Objects.equals(systemName, record.getSystemName())
                    && Objects.equals(module, record.getModule())
                    && set.add(showTitle)) {
                TreeNode node = new TreeNode();
                node.setId(showTitle);
                node.setText(showTitle);
                showTitles.add(node);
            }
        }
        return showTitles;
    }

    public static class Record {
        private String systemType;
        private String systemName;
        private String module;
        private String showTitle;

        public Record(String systemType, String systemName, String module, String showTitle) {
            this.systemType = systemType;
            this.systemName = systemName;
            this.module = module;
            this.showTitle = showTitle;
        }

        public String getSystemType() {
            return systemType;
        }

        public void setSystemType(String systemType) {
            this.systemType = systemType;
        }

        public String getSystemName() {
            return systemName;
        }

        public void setSystemName(String systemName) {
            this.systemName = systemName;
        }

        public String getModule() {
            return module;
        }

        public void setModule(String module) {
            this.module = module;
        }

        public String getShowTitle() {
            return showTitle;
        }

        public void setShowTitle(String showTitle) {
            this.showTitle = showTitle;
        }
    }

    public static class TreeNode {
        private String id;
        private String text;
        private List<TreeNode> children;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public List<TreeNode> getChildren() {
            return children;
        }

        public void setChildren(List<TreeNode> children) {
            this.children = children;
        }
    }
}
